/**
 * 好友列表碎片，负责展示好友列表并处理列表项点击事件
 */
package com.example.gitpart03.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.gitpart03.MainActivity2;
import com.example.gitpart03.R;
import com.example.gitpart03.adapter.FriendAdapter;
import com.example.gitpart03.entity.Friend;

import java.util.ArrayList;
import java.util.List;

public class FriendFragment extends Fragment{
    private List<Friend> friendData = new ArrayList<>();
    private FriendAdapter adapter;

    /**
     * 创建碎片视图，初始化好友列表数据并设置适配器
     * @param inflater 布局填充器
     * @param container 父容器
     * @param savedInstanceState 保存的实例状态
     * @return 碎片视图
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View mainView = inflater.inflate(R.layout.fragment_friend, container, false);

        initData();

        ListView gamesList = mainView.findViewById(R.id.list_games);
        FriendAdapter adapter = new FriendAdapter(getActivity(), R.layout.item_friend, friendData);
        gamesList.setAdapter(adapter);

        // 设置列表项点击事件，跳转到聊天界面
        gamesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Friend selectedGames = friendData.get(position);
                Intent intent = new Intent(getActivity(), MainActivity2.class);
                intent.putExtra("gameName", selectedGames.getName());
                intent.putExtra("gameImg", selectedGames.getImg());
                intent.putExtra("gameBrief", selectedGames.getBrief());
                startActivity(intent);
            }
        });
        return mainView;
    }

    /**
     * 初始化好友列表数据，添加示例好友信息
     */
    private void initData() {
        Friend zhanbao = new Friend(R.drawable.zhanbao,"","我的战报","上周你在好友击倒周榜中表现出了卓...");
        Friend xiaoxi = new Friend(R.drawable.xiaoxi,"","系统消息","本周福星好友已出炉，速度约起上分...");
        Friend mishu = new Friend(R.drawable.mishu,"","和平营地小助手","超值！精英币额外返利上线！");
        friendData.add(zhanbao);
        friendData.add(xiaoxi);
        friendData.add(mishu);
    }
}
